package idv.arthur.work;

import java.io.IOException;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.stream.JsonReader;

/**
 * Servlet implementation class SetMRTCmapping
 */
//@WebServlet(description = "設定測試項目與測試案例", urlPatterns = { "/SetMRTCmapping" })
public class SetMRTCmapping extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SetMRTCmapping() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String strSQL = "SELECT MAX(ver) FROM tms_mrtcmapping WHERE tpid=? AND mrid=?";
		String returnResult = "";
		int ver = 0;
		
		String jsonData = request.getParameter("selectTCList");
		JsonReader reader = new JsonReader(new StringReader(jsonData));
		int tpid = Integer.parseInt(request.getParameter("tpid"));
		String mrid = request.getParameter("mrid");
		String content ="";
		String creator = request.getParameter("loginUser");
		
		try {
			conn = wid.getConn(true);
			
			/** 先把目前的版次給記錄下來 **/
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, tpid);
			ps.setString(2, mrid);
			rs = ps.executeQuery();
			rs.next();
			ver = rs.getInt(1);	//這是目前現的的版次
			ver++;
			/** 將 mrtcmapping 的值刪除  **/
			strSQL = "DELETE FROM tms_mrtcmapping WHERE tpid=? AND mrid=?";
			ps = conn.prepareStatement(strSQL);
			ps.setInt(1, tpid);
			ps.setString(2, mrid);
			ps.execute();
			
			/** 加入現在的MAPPING值  **/
			reader.beginArray();
			if (reader.hasNext()) {
				while (reader.hasNext()) {
					content = reader.nextString();
					strSQL = "INSERT INTO activiti.tms_mrtcmapping (tpid,mrid,tcuid,ver) VALUES (?,?,?,?)";
					ps = conn.prepareStatement(strSQL);
					ps.setInt(1, tpid);
					ps.setString(2, mrid);
					ps.setString(3, content);
					ps.setInt(4, ver);	//跳下一個版本
					ps.execute();
				}
				strSQL = "UPDATE activiti.tms_tpitem SET isTCsubmitAble =1, isTCsubmitted=0, isTCreviewed=0, isTRsubmitAble=0, isTRsubmitted =0, isTRreviewed=0 WHERE tpid = ? AND mrid = ?";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, tpid);
				ps.setString(2, mrid);
				ps.executeUpdate();
				
				/** 寫入執行計錄 **/
				wid.keeplog(tpid, mrid, ver, 5, creator, conn, ps);
			} else {
				returnResult ="什麼都沒有!";
			}
			reader.endArray();
		} catch (IOException e) {
			wid.keepErrorMes(conn, ps, e, "", true);
		} catch (SQLException e) {
			returnResult = wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				returnResult = e.toString()+"\n";
			}
		}
		response.setContentType("application/text");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);
	}

}
